//https://leetcode.cn/submissions/detail/560651849/
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int n = s.length(); s = ' ' + s;
        using ll = long long;
        map<char, ll> cnt;
        ll l = 1, ans = 0;
        for (int r = 1; r <= n; r++)
        {
            while (cnt[s[r]] && s[r] != s[l])cnt[s[l]]--, l++;
            if (s[r] == s[l] && l != r)cnt[s[l]]--, l++;
            ans = max(ans, r - l + 1);
            cnt[s[r]]++;
        }
        return ans;
    }
};